Battle={}
BattleFunc={}
BattleFunc.__index = BattleFunc
setmetatable(Battle, BattleFunc)

local parserHurt = function(data)
  local pos = 0
  for i=1,string.len(data) do
    if string.byte(data, i)>=string.byte("0") and string.byte(data, i)<=string.byte("9") then
      pos = i
      break
    end
  end
  pos = pos-1
  return string.sub(data, pos, pos), string.sub(data, pos-string.len(data))
end
local parserFightScript = function(data)
  local seq = split(data, ";")
  if seq==nil then
    print("parserFightScript return nil", data)
    return nil
  end
  local fightSeq=Array:create()
  for i=0,seq:size()-1 do
    local ts = split(seq:at(i), "|")
    print("parserFightScript", ts, seq:at(i))
    local res = {}
    res.direction = string.sub(ts:at(1), 1, 1)
    res.attackType = string.sub(ts:at(1), 2, 2)
    res.attackPos = tonumber(string.sub(ts:at(2), 1, 1))
    res.defend = {}--Array:create()
    local tseq1 = split(ts:at(3), ",")
    local tseq2 = split(ts:at(4), ",")
    local tseq3 = split(ts:at(5), ",")

    if tseq3~=nil then res.attackBuf = tseq3:at(0) end
    for j=0, tseq1:size()-1 do
      local dt, hurt = parserHurt(tseq2:at(j))
      local a = { defendPos=tonumber(string.sub(tseq1:at(j), 1, 1)),
                defendType=dt,
                hurt=tonumber(hurt),
              }
      if tseq3~=nil then a.buf=tseq3:at(j+1) end
      for k,v in pairs(a) do print(k,v) end
      if a.defendPos~=0 then
        res.defend[a.defendPos] = a
      end
    end
    fightSeq:append(res)
  end
  return fightSeq
end
BattleFunc.create = function(self, data)
  local l = {}
  setmetatable(l, BattleFunc)
  l.battleData = data 
  l.fightscript = l.battleData.models.Campaign[1].script
  l.leftArmy = l.battleData.models.Army[1].soldierList
  l.rightArmy = l.battleData.models.Army[2].soldierList
  l.fightSeq = parserFightScript(l.fightscript)
  return l
end
BattleFunc.isVictory = function(self, data)
  local result = self.battleData.models.Campaign[1].result
  local cmd = self.battleData.cmd
  if result==1 then
    return true
  end
  return false
  --[[
  if (self.battleData.success~=nil and self.battleData.success==true) 
      or (self.battleData.data.success~=nil and self.battleData.data.success==true) then
    return true
  else
    return false
  end
  ]]
end
BattleFunc.getSoliderLost = function(self, index)
  local count = 0
  for k, v in pairs(self.battleData.models.Army[index].soldierList) do
    count = count + (v.blood-v.curBlood)
  end
  return count
end
BattleFunc.getRewardItems = function(self)
  local rewards = {}
  local battleType = self.battleData.models.Campaign[1].defendLord.type
  if battleType=="checkpoint" or battleType=="conquer" then
    rewards[#rewards+1] = {t="cash", v=self.battleData.data.gainCash}
    rewards[#rewards+1] = {t="xp", v=self.battleData.data.gainXp}
    if self.battleData.data.drops~=nil then
      for k,v in ipairs(self.battleData.data.drops) do
        rewards[#rewards+1] = {t=v.type, v=v}
      end
    end
  elseif battleType=="player" and self.battleData.cmd=="/game/newAthletics/fight" then
  --竞技场
    rewards[#rewards+1] = {t="cash", v=self.battleData.data.gainCash}
    rewards[#rewards+1] = {t="reputation", v=self.battleData.data.gainReputation}
  end  
  return rewards
end
BattleFunc.getEvaluate = function(self)
  return self.battleData.data.evaluate
end
